package lovo.practice_7_14.frame;

import lovo.Util.Util;
import lovo.practice_7_14.pojo.UserDO;
import lovo.practice_7_14.service.UserService;
import lovo.practice_7_14.service.impl.UserService.UserServiceImpl;
import lovo.practice_7_14.util.DBUtil;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TransPanel extends JPanel implements ActionListener {
    private J189_7_14 frame;
    private JButton jButtonTrans;
    private JTextField jTextFieldwho;
    private JTextField jTextFieldhow;
    private JButton jButtonBack;
    private Icon imgback = Util.getimagesIcon("/Images/buttonOff.jpg");

    public TransPanel(J189_7_14 frame) {
        this.frame = frame;
        this.setLayout(null);
        this.setOpaque(false);
        jButtonTrans = new JButton("转账");
        jButtonTrans.setBounds(200, 200, 140, 50);
        this.add(jButtonTrans);

        jTextFieldwho = new JTextField();
        jTextFieldwho.setBounds(400, 200, 140, 50);
        this.add(jTextFieldwho);

        jTextFieldhow = new JTextField();
        jTextFieldhow.setBounds(400, 400, 140, 50);
        this.add(jTextFieldhow);

        jButtonBack = new JButton(imgback);
        jButtonBack.setBounds(600, 600, 140, 50);
        this.add(jButtonBack);
        jButtonTrans.addActionListener(this);
        jTextFieldwho.addActionListener(this);
        jTextFieldhow.addActionListener(this);
        jButtonBack.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == jButtonTrans) {
            // 获取输入的对方账号
            String otherAccount = jTextFieldwho.getText();
//            DBUtil dbUtil = new DBUtil();
//            UserDO loginUserDO = dbUtil.getById(frame.getUsers().getAccount());
//            UserDO otherUserDO = dbUtil.getById(otherAccount);
            double blance = 0;
            try {
                // 获取输入的转账金额
                blance = Double.parseDouble(jTextFieldhow.getText());
            } catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(this, "转账金额必须为数字，并不能为空");
                return;
            }
//
//            // 判断账号是否存在
//            if (otherUserDO == null) {
//                JOptionPane.showMessageDialog(this, "查无此人");
//                return;
//            }
//            // 判断是否转给自己
//            if (loginUserDO.getAccount().equals(otherAccount)) {
//                JOptionPane.showMessageDialog(this, "不能转给自己");
//                return;
//            }
//            // 判断余额是否足够
//            if (blance > loginUserDO.getBalance()) {
//                JOptionPane.showMessageDialog(this, "余额不足");
//                return;
//            }
//            loginUserDO.setBalance(loginUserDO.getBalance() - blance);
//            otherUserDO.setBalance(otherUserDO.getBalance() + blance);
//
//            try {
//                dbUtil.getConnection();
//                dbUtil.setAutoCommit(false);
//                dbUtil.update1(loginUserDO);
//                dbUtil.update1(otherUserDO);
//                dbUtil.commit();
//                JOptionPane.showMessageDialog(this, "转账成功");
//                frame.switchPanel("menu");
//            } catch (Exception e1) {
//                dbUtil.rollback();
//                JOptionPane.showMessageDialog(this, "转账失败");
//            }finally {
//                dbUtil.close();
//            }
            UserService userService=new UserServiceImpl();
            try {
                userService.transMoney(frame.getUsers().getAccount(),otherAccount,blance);
                JOptionPane.showMessageDialog(this, "转账成功");
            } catch (Exception exception) {
                exception.printStackTrace();
            }

        }
        if (e.getSource() == jButtonBack) {
            frame.switchPanel("mune");
        }
    }
}
